package com.qn.controller;

import com.qn.common.result.CommonResult;
import com.qn.entity.I18nMsg;
import com.qn.service.I18nMsgService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.qn.common.code.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
import static com.qn.common.code.util.ServiceExceptionUtil.exception;


/**
 * 测试国际化
 *
 * @author QN
 * @date 2025/01/01
 */
@Validated
@RestController
@RequestMapping("/demo/i18n")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestI18nController {

    private final I18nMsgService i18nMsgService;

    /**
     * 测试返回结果
     *
     * @return 国际化
     */
    @GetMapping("/test/result")
    public CommonResult<String> testResult() {
        return CommonResult.error(INTERNAL_SERVER_ERROR);
    }

    /**
     * 测试异常
     *
     * @return 国际化
     */
    @GetMapping("/test/exception")
    public void testException() {
        throw exception(INTERNAL_SERVER_ERROR);
    }

    /**
     * Validator 校验国际化
     * 不传值 分别查看异常返回
     * <p>1001201007
     * 测试使用 DEMO03_STUDENT_NOT_EXISTS
     */
    @GetMapping("/test1")
    public String test1(@NotBlank(message = "{DEMO03_STUDENT_NOT_EXISTS}") String str) {
        return str;
    }

    /**
     * Bean 校验国际化
     * 不传值 分别查看异常返回
     * <p>
     * 测试使用 DEMO03_STUDENT_NOT_EXISTS
     */
    @GetMapping("/test2")
    public TestI18nBo test2(@Validated TestI18nBo bo) {
        return bo;
    }

    /**
     * 创建国际化
     *
     * @param i18nMsg i18nMsg
     * @return {@link CommonResult }<{@link String }>
     */
    @PostMapping("/create")
    public CommonResult<Boolean> createI18nMsg(@RequestBody I18nMsg i18nMsg) {
        i18nMsgService.saveI18nMsg(i18nMsg);
        return CommonResult.success(true);
    }

    @PostMapping("/update")
    public CommonResult<Boolean> updateI18nMsg(@RequestBody I18nMsg i18nMsg) {
        i18nMsgService.updateI18nMsg(i18nMsg);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult<Boolean> deleteI18nMsg(@PathVariable("id") Long id) {
        i18nMsgService.deleteI18nMsg(id);
        return CommonResult.success(true);
    }

    @Data
    public static class TestI18nBo {

        @NotBlank(message = "{DEMO03_STUDENT_NOT_EXISTS}")
        private String name;

        @NotNull(message = "{DEMO03_GRADE_NOT_EXISTS}")
//        @Range(min = 0, max = 100, message = "{length.not.valid}")
        private Integer age;
    }
}
